package com.linkai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkai.dto.admin.business.sale.ProductShowDto;
import com.linkai.dto.admin.business.sale.SaleOrderIndexDto;
import com.linkai.dto.admin.business.sale.SaleOrderSaveDto;
import com.linkai.entity.Saleorder;
import com.linkai.entity.SaleorderProduct;
import com.linkai.entity.Storage;
import com.linkai.mapper.*;
import com.linkai.service.SaleorderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yamon
 * @since 2021-03-10
 */
@Service
public class SaleorderServiceImpl extends ServiceImpl<SaleorderMapper, Saleorder> implements SaleorderService {

    @Autowired
    private SaleorderProductMapper saleorderProductMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private MoneyaccountMapper moneyaccountMapper;
    @Autowired
    private StorageMapper storageMapper;
    /**
     * 列出所有的销售单给首页
     *
     * @return list
     */
    @Override
    public List<SaleOrderIndexDto> listAllSaleOrder4Index() {
        //找出销售单
        final List<Saleorder> saleorderList = this.list();
        return saleorderList.stream().map(saleOrder -> {
            final SaleOrderIndexDto saleOrderIndexDto = new SaleOrderIndexDto();
            BeanUtils.copyProperties(saleOrder, saleOrderIndexDto);
            //设置人
            saleOrderIndexDto.setEmpName(employeeMapper.selectById(saleOrder.getEmpId()).getName());
            saleOrderIndexDto.setCusName(customerMapper.selectById(saleOrder.getCusId()).getName());
            saleOrderIndexDto.setAccName(moneyaccountMapper.selectById(saleOrder.getAccId()).getAccName());
            //设置数量
            final QueryWrapper<SaleorderProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_id", saleOrder.getId());
            final List<SaleorderProduct> saleorderProducts = saleorderProductMapper.selectList(queryWrapper);
            int count = 0;
            for (SaleorderProduct saleorderProduct : saleorderProducts) {
                count += saleorderProduct.getProNum();
            }
            saleOrderIndexDto.setProductNum(count);
            return saleOrderIndexDto;
        }).collect(Collectors.toList());
    }

    /**
     * 查看一个销售单的详情
     *
     * @param id id
     * @return dto
     */
    @Override
    public SaleOrderSaveDto viewOneSaleOrderById(Integer id) {
        Saleorder saleorder = this.getById(id);
        SaleOrderSaveDto saleOrderSaveDto = new SaleOrderSaveDto();
        BeanUtils.copyProperties(saleorder,saleOrderSaveDto);
        //设置
        saleOrderSaveDto.setEmpName(employeeMapper.selectById(saleorder.getEmpId()).getName());
        saleOrderSaveDto.setCusName(customerMapper.selectById(saleorder.getCusId()).getName());
        saleOrderSaveDto.setAccName(moneyaccountMapper.selectById(saleorder.getAccId()).getAccName());
        //设置商品
        final ArrayList<ProductShowDto> products = new ArrayList<>();
        final QueryWrapper<SaleorderProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",id);
        final List<SaleorderProduct> saleorderProducts = saleorderProductMapper.selectList(queryWrapper);
        final List<ProductShowDto> productShowDtos = saleorderProducts.stream().map(saleorderProduct -> {
            Storage storage = storageMapper.selectById(saleorderProduct.getStorageId());
            ProductShowDto productShowDto = new ProductShowDto();
            productShowDto.setId(saleorderProduct.getStorageId());
            productShowDto.setProTitle(storage.getProTitle());
            productShowDto.setProNum(saleorderProduct.getProNum());
            productShowDto.setProUnit(storage.getProUnit());
            productShowDto.setProPrice(saleorderProduct.getProPrice());
            productShowDto.setEachSum(saleorderProduct.getEachSum());
            return productShowDto;
        }).collect(Collectors.toList());
        saleOrderSaveDto.setProducts(productShowDtos);
        return saleOrderSaveDto;
    }

    /**
     * 出库销售单
     *
     * @param orderId 销售单id
     * @return boolean
     */
    @Override
    public boolean outputStorageProductBySaleOrderId(Integer orderId) {
        final Saleorder entity = new Saleorder();
        entity.setId(orderId);
        entity.setInpState(1);
        this.updateById(entity);
        return this.outputStorageBySaleOrderId(orderId);
    }


    /**
     * 出库
     * @param saleOrderId 订单id
     */
    public boolean outputStorageBySaleOrderId(Integer saleOrderId){
        //查找出来所有的product
        final QueryWrapper<SaleorderProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",saleOrderId);
        final List<SaleorderProduct> saleorderProducts = saleorderProductMapper.selectList(queryWrapper);
        int count = 0;
        for (SaleorderProduct saleorderProduct : saleorderProducts) {
            saleorderProduct.setProState(1);
            saleorderProductMapper.updateById(saleorderProduct);
            final Storage storage = storageMapper.selectById(saleorderProduct.getStorageId());
            storage.setProNum(storage.getProNum()-saleorderProduct.getProNum());
            storage.setProMoney(storage.getProMoney().subtract(saleorderProduct.getEachSum()));
            storage.setState(1);
            int success = storageMapper.updateById(storage);
            count+=success;
        }
        return count==saleorderProducts.size();
    }
}
